package com.whsxt.buss.service.impl;

import com.whsxt.buss.domain.BusGoods;
import com.whsxt.buss.domain.BusSales;
import com.whsxt.buss.domain.BusSalesback;
import com.whsxt.buss.mapper.BusGoodsMapper;
import com.whsxt.buss.mapper.BusSalesMapper;
import com.whsxt.buss.mapper.BusSalesbackMapper;
import com.whsxt.buss.service.IBusSalesService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.Result;
import com.whsxt.common.util.WebContextUtil;
import com.whsxt.sys.domain.User;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-28
 */
@Service
@Transactional
public class BusSalesServiceImpl extends ServiceImpl<BusSalesMapper, BusSales> implements IBusSalesService {
	
	
	
	@Autowired
	private BusGoodsMapper busGoodsMapper;
	
	@Autowired
	private BusSalesbackMapper busSalesbackMapper;
	
	@Override
	public Object addSales(BusSales sales) {
		Integer number = sales.getNumber();
		Integer goodsid = sales.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		Integer number2 = goods.getNumber();
		if(number2 < number) {
			return new Result(CodeMsg.LOW_STOCK);
		}
		goods.setNumber(number2-number);
		busGoodsMapper.updateById(goods);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		sales.setOperateperson(user.getName());
		sales.setSalestime(new Date());
		this.getBaseMapper().insert(sales);
		return new Result(CodeMsg.SALES_ADD_SECCUSS);
	}


	@Override
	public Result updateSales(BusSales sales) {
		//更新的销售数量
		Integer number = sales.getNumber();
		Integer id = sales.getId();
		BusSalesMapper baseMapper2 = this.getBaseMapper();
		BusSales selectById = baseMapper2.selectById(id);
		//之线的销售数据
		Integer number2 = selectById.getNumber();
		Integer goodsid = sales.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		//库存
		Integer number3 = goods.getNumber();
		if(number > number3) {
			return new Result(CodeMsg.LOW_STOCK);
		}
		goods.setNumber(number3+number2-number);
		busGoodsMapper.updateById(goods);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		sales.setOperateperson(user.getName());
		sales.setSalestime(new Date());
		this.getBaseMapper().updateById(sales);
		return new Result(CodeMsg.SALES_DELETE_SECCUSS);
	}


	@Override
	public Result deleteOne(Integer id) {
		BusSalesMapper baseMapper = this.getBaseMapper();
		BusSales sales = baseMapper.selectById(id);
		//删除之前的数量
		Integer number = sales.getNumber();
		Integer goodsid = sales.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		//库存
		 Integer number2 = goods.getNumber();
		 goods.setNumber(number+number2);
		 busGoodsMapper.updateById(goods);
		 baseMapper.deleteById(id);
		return new Result(CodeMsg.SALES_DELETE_SECCUSS);
	}


	@Override
	public Result back(Integer id, Integer number, String remark) {
		BusSalesMapper baseMapper = this.getBaseMapper();
		BusSales sales = baseMapper.selectById(id);
		//之前数量
		Integer number2 = sales.getNumber();
		if(number2 < number) {
			return new Result(CodeMsg.SALES_NUMBER_ERROR);
		}
		Integer goodsid = sales.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		//库存
		Integer number3 = goods.getNumber();
		goods.setNumber(number3+number2-number);
		busGoodsMapper.updateById(goods);
		BusSalesback busSalesback = new BusSalesback();
		busSalesback.setCustomerid(sales.getCustomerid());
		busSalesback.setGoodsid(goodsid);
		busSalesback.setNumber(number);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		busSalesback.setOperateperson(user.getName());
		busSalesback.setPaytype(sales.getPaytype());
		busSalesback.setRemark(remark);
		busSalesback.setSalebackprice(sales.getSaleprice());
		busSalesback.setSalesbacktime(new Date());
		busSalesbackMapper.insert(busSalesback);
		return new Result(CodeMsg.SALES_BACK_SECCUSS);
	}


}
